package com.ruoyi.performance.service.impl;

import com.google.common.base.Joiner;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.performance.domain.PerfCheckType;
import com.ruoyi.performance.domain.PerfCheckTypeSupplier;
import com.ruoyi.performance.domain.PerfCheckreport;
import com.ruoyi.performance.dto.PerfCheckTypeDto;
import com.ruoyi.performance.dto.PerfCheckTypeSupplierDto;
import com.ruoyi.performance.mapper.PerfCheckTypeMapper;
import com.ruoyi.performance.service.PerfCheckTypeService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author YL
 * @date 2020/9/5 23:48
 * @description TODO
 */
@Service
@Slf4j
public class PerfCheckTypeServiceImpl implements PerfCheckTypeService {

    @Resource
    private PerfCheckTypeMapper perfCheckTypeMapper;

    /**
     * 查询考核类别
     *
     * @return 考核类别
     */
    @Override
    public List<PerfCheckType> selectPerfCheckType(PerfCheckTypeDto perfCheckTypeDto) {
        return perfCheckTypeMapper.selectPerfCheckType(perfCheckTypeDto);
    }

    /**
     * 查询考核类别
     *
     * @param id 考核类别ID
     * @return 考核类别
     */
    @Override
    public PerfCheckType selectPerfCheckTypeById(int id) {
        return perfCheckTypeMapper.selectPerfCheckTypeById(id);
    }

    /**
     * 查询供应商
     *
     * @param checkTypeId 考核类别ID
     * @return 考核类别
     */
    @Override
    public List<PerfCheckTypeSupplierDto> selectPerfCheckTypeSupplierDtoById(int checkTypeId) {
        return perfCheckTypeMapper.selectPerfCheckTypeSupplierDtoById(checkTypeId);
    }

    /**
     * 新增考核类别
     *
     * @param perfCheckTypeDto 考核类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int insertPerfCheckType(PerfCheckTypeDto perfCheckTypeDto) {
    	perfCheckTypeDto.setCheckTypeName(perfCheckTypeDto.getCheckTypeName());
    	perfCheckTypeDto.setCheckTypeDescription(perfCheckTypeDto.getCheckTypeDescription());
        int row=perfCheckTypeMapper.insertPerfCheckType(perfCheckTypeDto);
        return row;
    }
    
    
    /**
     * 新增考核类别与供应商关系
     *
     * @param perfCheckTypeDto 考核类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public String insertTypeVendor(Map<String, List<PerfCheckTypeSupplier>> datas) {
    	String result = "1";
		List<String> check = new ArrayList<String>();
		for (PerfCheckTypeSupplier perfCheckTypeSupplier : datas.get("datas"))
		{
		    try {
		    	perfCheckTypeMapper.insertPerfCheckTypeSupplier(perfCheckTypeSupplier);
			} catch (Exception e) {
				result = "新增供应商" + perfCheckTypeSupplier.getVendorname() + "失败,请联系管理员！";
				log.error(result, e);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return result;
			}
		}
		if (check.size() > 0 && check != null) {
			result = Joiner.on(",").join(check);
		}
        return result;
    }

    /**
     * 修改考核类别
     *
     * @param perfCheckTypeDto 考核类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updatePerfCheckType(PerfCheckTypeDto perfCheckTypeDto) {
        PerfCheckType perfCheckType = new PerfCheckType();
        perfCheckType.setId(perfCheckTypeDto.getId());
        perfCheckType.setCheckTypeName(perfCheckTypeDto.getCheckTypeName());
        perfCheckType.setCheckTypeDescription(perfCheckTypeDto.getCheckTypeDescription());
        int row=perfCheckTypeMapper.updatePerfCheckType(perfCheckType);
		/*
		 * List<PerfCheckTypeSupplierDto> suppliers=perfCheckTypeDto.getSupplier();
		 * System.out.println(suppliers.size()); if (suppliers.size()!=0){
		 * perfCheckTypeMapper.deletePerfCheckTypeSupplier(perfCheckTypeDto.getId());
		 * PerfCheckTypeSupplier perfCheckTypeSupplier = new PerfCheckTypeSupplier();
		 * perfCheckTypeSupplier.setCheckTypeId(perfCheckType.getId());
		 * suppliers.forEach(supplier->{
		 * perfCheckTypeSupplier.setVendorId(supplier.getSupplierId());
		 * perfCheckTypeMapper.insertPerfCheckTypeSupplier(perfCheckTypeSupplier); }); }
		 */
        return row;
    }

    /**
     * 删除考核类别
     *
     * @param id 考核类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public String deletePerfCheckType(String ids) {
    	
        String result = "1";
		List<String> checkstatus = new ArrayList<String>();
		String[] id = ids.split(",");
		for (String typeId : id) {
			int typeIdI = Integer.parseInt(typeId);

			if (!perfCheckTypeMapper.checkTypeUse(typeIdI)) {
				try {
					perfCheckTypeMapper.deletePerfCheckType(typeIdI);
					perfCheckTypeMapper.deletePerfCheckTypeSupplier(typeIdI);
				} catch (Exception e) {
					result = "删除类别失败,类别Id" + typeIdI + ",请联系管理员！";
					log.error(result, e);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return result;
				}
			} else {
				result = "考核类别" + perfCheckTypeMapper.selectPerfCheckTypeById(typeIdI).getCheckTypeName() +  "已在考核单中使用，禁止删除！";
				checkstatus.add(result);
			}
		}
		if (checkstatus.size() > 0 && checkstatus != null) {
			result = Joiner.on(",").join(checkstatus);
		}
		return result;
    }
    
    /**
     * 删除考核类别与供应商关系
     *
     * @param id 考核类别
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public String deleteTypeVencode(String ids) {
    	
        String result = "1";
		List<String> checkstatus = new ArrayList<String>();
		String[] id = ids.split(",");
		for (String typeId : id) {
			int typeIdI = Integer.parseInt(typeId);

				try {
					perfCheckTypeMapper.deleteSupplier(typeIdI);
				} catch (Exception e) {
					result = "删除类别与供应商关系失败，Id" + typeIdI + ",请联系管理员！";
					log.error(result, e);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return result;	
			} 
		}
		if (checkstatus.size() > 0 && checkstatus != null) {
			result = Joiner.on(",").join(checkstatus);
		}
		return result;
    }
}
